/**
 * 宿舍管理 API
 */

import { get, post, put, del, upload, download } from '@/utils/request'

/**
 * 获取宿舍列表
 * @param {Object} params { page, pageSize, building, roomNumber }
 * @returns {Promise}
 */
export function getDormitoryList(params) {
    return get('/dormitory/list', params)
}

/**
 * 获取宿舍详情
 * @param {String|Number} id 宿舍ID
 * @returns {Promise}
 */
export function getDormitoryDetail(id) {
    return get('/dormitory/detail', { id })
}

/**
 * 添加宿舍
 * @param {Object} data 宿舍信息
 * @returns {Promise}
 */
export function addDormitory(data) {
    return post('/dormitory/add', data)
}

/**
 * 更新宿舍信息
 * @param {String|Number} id 宿舍ID
 * @param {Object} data 宿舍信息
 * @returns {Promise}
 */
export function updateDormitory(id, data) {
    return post('/dormitory/update', { ...data, id })
}

/**
 * 删除宿舍
 * @param {String|Number} id 宿舍ID
 * @returns {Promise}
 */
export function deleteDormitory(id) {
    return del(`/dormitory/delete/${id}`)
}

/**
 * 批量删除宿舍
 * @param {Array} ids 宿舍ID数组
 * @returns {Promise}
 */
export function batchDeleteDormitories(ids) {
    return post('/dormitory/batch-delete', ids)
}

/**
 * 导入宿舍数据
 * @param {File} file Excel 文件
 * @param {Function} onProgress 上传进度回调
 * @returns {Promise}
 */
export function importDormitories(file, onProgress) {
    const formData = new FormData()
    formData.append('file', file)
    return upload('/dormitory/import', formData, onProgress)
}

/**
 * 导出宿舍数据
 * @param {Object} params 搜索条件
 * @returns {Promise}
 */
export function exportDormitories(params = {}) {
    const filename = `宿舍列表_${new Date().toISOString().slice(0, 10)}.xlsx`
    return download('/dormitory/export', params, filename)
}

/**
 * 下载宿舍导入模板
 * @returns {Promise}
 */
export function downloadDormitoryTemplate() {
    return download('/dormitory/template', {}, '宿舍信息导入模板.xlsx')
}

/**
 * 获取宿舍统计信息
 * @returns {Promise}
 */
export function getDormitoryStatistics() {
    return get('/dormitory/statistics')
}

/**
 * 根据楼栋获取可用宿舍
 * @param {String} building 楼栋名称
 * @returns {Promise}
 */
export function getAvailableDormitories(building) {
    return get('/dormitory/available', { building })
}

